//
//  BattleUILayer.m
//  SpaceStorm
//
//  Created by 祥 雍 on 13-4-10.
//  Copyright (c) 2013年 __MyCompanyName__. All rights reserved.
//


#import "BattleUILayer.h"
#import "Constants.h"

@implementation BattleUILayer

-(id) init
{
	// always call "super" init
	// Apple recommends to re-assign "self" with the "super" return value
	if( (self=[super init])) 
    {
        size = [[CCDirector sharedDirector] winSize];
        
        //初始化自己总战斗力槽
        _leftEquipBlood = [SlowBlood bloodWithBG:@"equipBloodBG.png" slow:@"equipBloodSlowBG.png" blood:@"equipBlood.png"];
        _leftEquipBlood.position = ccp(60 + _leftEquipBlood.width /2, size.height - _leftEquipBlood.height /2);
        [self addChild:_leftEquipBlood];
        
        //初始化地方总战斗力槽
        _rightEquipBlood = [SlowBlood bloodWithBG:@"equipBloodBG.png" slow:@"equipBloodSlowBG.png" blood:@"equipBlood.png"];
        _rightEquipBlood.position = ccp(size.width - 60 - _leftEquipBlood.width/2, size.height - _leftEquipBlood.height /2);
        _rightEquipBlood.scaleX = -1;
        [self addChild:_rightEquipBlood];
        
        //战斗倒计时
        _leftTimeLabel = [CCLabelTTF labelWithString:@"00:00" fontName:@"Marker Felt" fontSize:12];
        _leftTimeLabel.anchorPoint = ccp(0, 0.5);
        _leftTimeLabel.position = ccp(224, 296);
        [self addChild:_leftTimeLabel];
        
        //初始化船舰信息
        _infoItem = nil;
        
        left = 3600;
        [self schedule:@selector(secondUpdate) interval:1.0f];
    }
    
    return self;
}

/**
    更新左右2边的船舰列表
	@param leftArray 左边的船舰列表的数组，struct ListItemVo的NSValue类型
	@param rightArray 右边的船舰列表的数组
 */
-(void) updateList:(CCArray*)leftArray rightArray:(CCArray*)rightArray
{
    _leftViewList = [ShipListView listWithVoArray:leftArray color:ccc4(255, 0, 0, 0) width:60 height:size.height];
    [self addChild:_leftViewList];
    
    _rightViewList = [ShipListView listWithVoArray:rightArray color:ccc4(0, 255, 0, 0) width:60 height:size.height];
    _rightViewList.position = ccp(size.width - 60, 0);
    [self addChild:_rightViewList];
}

/**
	更新战斗倒计时
	@param leftTime 剩余时间
 */
-(void) updateLeftTime:(ccTime)leftTime
{
    int minute;
    int second;
    NSString* minuteStr;
    NSString* secondStr;
    
    //计算倒计时的分和秒
    if(leftTime > 3600)
    {
        minute = 60;
        second = 0;
        
    }
    else if(leftTime < 0)
    {
        minute = 0;
        second = 0;
    }
    else
    {
        minute = floor(leftTime/60);
        second = fmod(leftTime, 60);
    }
    
    //小于10，补2位对齐
    if(minute < 10)
    {
        minuteStr = [NSString stringWithFormat:@"0%d", minute];
    }
    else
    {
        minuteStr = [NSString stringWithFormat:@"%d", minute];
    }
    
    if(second < 10) 
    {
        secondStr = [NSString stringWithFormat:@"0%d", second];
    }
    else
    {
        secondStr = [NSString stringWithFormat:@"%d", second];
    }
    
    
    //设置剩余时间
    [_leftTimeLabel setString:[NSString stringWithFormat:@"%@:%@", minuteStr, secondStr]];
}


- (void)secondUpdate
{
    left--;
    [self updateLeftTime:left];
}

- (void) dealloc
{
    
	// don't forget to call "super dealloc"
	[super dealloc];
}

/**
	选中一艘船
	@param ship 船的指针
 */
-(void) selectShip:(Ship*)ship
{
    if(_infoItem != nil)
    {
        [_infoItem.parent removeChild:_infoItem cleanup:YES];
        _infoItem = nil;
    }
    
    //船舰信息框
    _infoItem = [BattleShipInfoItem shipItemWidthShip:ship];
    //根据船的角度算出面板的位置
    [self addChild:_infoItem];
}

/**
	 取消选择所有的船
 */
-(void) unselectAll
{
    
}

@end
